package com.example.zjhuojback.service;

import com.example.zjhuojback.entity.Forum;
import com.example.zjhuojback.entity.User;
import com.example.zjhuojback.exception.ServiceException;
import com.example.zjhuojback.mapper.ForumMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ForumService {
    @Autowired
    ForumMapper forumMapper;
//    public List<Forum> selectAll() {
//        return forumMapper.selectAll();
//    }
    public List<Forum> selectAll(int page, int size) {
        int offset = (page - 1) * size;  // 计算偏移量
        return forumMapper.selectAll(offset, size);
    }

    public long countAll() {
        return forumMapper.countAll();  // 查询总记录数
    }

    public void insertForum(Forum forum) {
        forumMapper.insert(forum);
    }
    public void updataForum(Forum forum) {
        LocalDateTime loginTime = LocalDateTime.now(); // 获取当前时间作为登录时间
        forum.setUpdateTime(loginTime);
        forumMapper.update(forum);
    }

    public void addcomment(Forum forum) {
        forumMapper.addcomment(forum);
    }
    /**
     * 查找帖子
     * @param id
     * @return
     */
    public Forum selectById(Integer id) {
        // 查询信息
        Forum dbForum = forumMapper.selectById(id);
        return dbForum;
    }

    public List<Forum> getCommentsByPostId(Integer id) {
        // 查询信息
        return forumMapper.getCommentsByPostId(id);
    }

    public void deleteComment(Integer id) {
        forumMapper.deleteCommentByCommentId(id);
    }

    public void delete(Integer id) {
        forumMapper.deleteComment(id);
        forumMapper.deleteForum(id);
    }

    public void incrementThumbNumById(Integer id) {
        forumMapper.incrementThumbNumById(id);
    }

    public void incrementFavourNumById(Integer id) {
        forumMapper.incrementFavourNumById(id);
    }
}
